package cn.com.tiptop.license.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.tiptop.util.FileUtils;
import cn.com.tiptop.util.HardwareUtil;
import cn.com.tiptop.util.RSAUtils;

@Controller
@RequestMapping("/exportProducInfo")
public class ExportProducInfo {

	@SuppressWarnings("deprecation")
	@RequestMapping("/download")
	public void download(HttpServletRequest request, HttpServletResponse response) {
		String path = request.getRealPath("")+"WEB-INF"+File.separatorChar+"download"+File.separatorChar+UUID.randomUUID().toString();
		System.out.println(path);
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		try {
			while(new File(path).exists()) {
				path = request.getRealPath("")+"WEB-INF"+File.separatorChar+"download"+File.separatorChar+UUID.randomUUID().toString();
			}
			Properties productInfo = HardwareUtil.getProductInformation();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			///保存属性到b.properties文件
			FileOutputStream oFile = new FileOutputStream(path, false);//true表示追加打开
			productInfo.store(oFile,"The New properties file");
			oFile.close();
			RSAUtils.RSAEncodeFile(RSAUtils.getPublicKey(RSAUtils.getPublicKey()), path);
			//产品号
			FileUtils.appendMethodFile(path, productInfo.getProperty("PRODUCT_NUMBER"));
			String fileName = productInfo.getProperty("PRODUCT_NAME")+"v"+productInfo.getProperty("PRODUCT_VERSION")+sdf.format(new Date())+".tiptop";
			
			File file = new File(path);
			
			if (file.exists()) {
				// 设置为下载application/x-download
				response.setContentType("application/x-download charset=UTF-8"); // 设置response内容的类型
				// 通常解决汉字乱码方法用URLEncoder.encode(...)
				String filenamedisplay = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
				if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") >= 0) {
					// 针对火狐浏览器处理方式不一样了
					filenamedisplay = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
				}
				response.setHeader("Content-Disposition", "attachment;filename=" + filenamedisplay);// 设置头部信息
				byte[] buffer = new byte[8192];// 缓存区大小
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				OutputStream os = response.getOutputStream();
				int i = bis.read(buffer);
				while (i != -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
				if (bis != null) {
					bis.close();
				}
				if (fis != null) {
					fis.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			File f = new File(path);
			if(f.exists()) {
				f.delete();
			}
		}
	}
}